package com.example.shishaolong.warehousemanager;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.Toast;

import com.example.shishaolong.warehousemanager.common.DateUtil;
import com.example.shishaolong.warehousemanager.common.SystemConst;
import com.example.shishaolong.warehousemanager.dao.AdminDao;
import com.example.shishaolong.warehousemanager.entity.AdminEntity;
import com.example.shishaolong.warehousemanager.view.TitleLayout;

public class AdminAddActivity extends AppCompatActivity {

    private AdminDao adminDao;
    private TitleLayout titleLayout;
    private String gotoFrom;
    private AdminEntity adminEntity;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_admin_add);

        //隐藏标题栏
        ActionBar actionBar = getSupportActionBar();
        if (actionBar != null) {
            actionBar.hide();
        }

        //界面绑定
        final EditText adminName = (EditText) findViewById(R.id.admin_name_value);
        final EditText adminPassword = (EditText) findViewById(R.id.admin_password_value);
//        final EditText adminSex = (EditText) findViewById(R.id.admin_sex_value);
        final RadioButton sexMan = (RadioButton) findViewById(R.id.admin_sex_man);
        final RadioButton sexWoman = (RadioButton) findViewById(R.id.admin_sex_woman);
        final RadioButton levelAdmin = (RadioButton) findViewById(R.id.admin_level_admin);
        final RadioButton superLevelAdmin = (RadioButton) findViewById(R.id.admin_level_super_admin);
        final EditText adminAddDate = (EditText) findViewById(R.id.admin_add_date_value);
        final EditText adminModifyDate = (EditText) findViewById(R.id.admin_modify_date_value);

        //创建数据实体
        adminDao = new AdminDao(getApplicationContext());
        final Intent intent = getIntent();
        gotoFrom = intent.getStringExtra(SystemConst.ADMIN_GOTO_FROM);
        if (SystemConst.ADMIN_GOTO_FROM_UPDATE.equals(gotoFrom)) {  //修改，需要给页面组件写入值
            setEntityToUi(adminName, adminPassword, sexMan, sexWoman, levelAdmin, superLevelAdmin, adminAddDate, adminModifyDate, intent);
        }else if(SystemConst.ADMIN_GOTO_FROM_USER_INFO.equals(gotoFrom)){ //从修改个人信息页面进入
            setEntityToUi(adminName, adminPassword, sexMan, sexWoman, levelAdmin, superLevelAdmin, adminAddDate, adminModifyDate, intent);
            //修改个人信息，不可更改管理员级别，管理员级别只能是超管更改
            levelAdmin.setEnabled(false);
            superLevelAdmin.setEnabled(false);
        } else { //添加  创建货物实体类
            adminEntity = new AdminEntity();
            adminAddDate.setText(DateUtil.getCurrentDateStr());
            adminModifyDate.setText(DateUtil.getCurrentDateStr());
        }

        //设置标题栏
        titleLayout = (TitleLayout) findViewById(R.id.title_layout);
        titleLayout.setTitle("管理员信息");
        titleLayout.setRightButton(new TitleLayout.ButotonEntity("保存", new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                //封装数据实体
                adminEntity.setName(adminName.getText().toString());
                adminEntity.setPassword(adminPassword.getText().toString());
                adminEntity.setSex(sexMan.isChecked() ? 1 : 2);  //1: 男  2：女
                adminEntity.setLevel(superLevelAdmin.isChecked() ? 1 : 2); //1：超管 2：普通管理员
                adminEntity.setModifyDate(adminModifyDate.getText().toString());
                adminEntity.setAddDate(adminAddDate.getText().toString());

                //保存信息：分为两种添加和修改的  根据tid判断
                if (SystemConst.ADMIN_GOTO_FROM_ADD.equals(gotoFrom)) {
                    adminDao.add(adminEntity);
                } else {
                    adminDao.update(adminEntity);
                }
                Toast.makeText(getApplicationContext(), "保存成功！", Toast.LENGTH_SHORT).show();

                //返回到管理员列表主界面，指定返回结果
                Intent gotoAdminActivityIntent = new Intent();//返回到货物列表的intent
                setResult(SystemConst.ADMIN_INFO_ACTIVITY_START_FOR_RESULT, gotoAdminActivityIntent); //设置页面标识符

                finish();
            }
        }));
    }

    private void setEntityToUi(EditText adminName, EditText adminPassword, RadioButton sexMan, RadioButton sexWoman, RadioButton levelAdmin, RadioButton superLevelAdmin, EditText adminAddDate, EditText adminModifyDate, Intent intent) {
        int id = intent.getExtras().getInt(SystemConst.ADMIN_ENTITY_ID);
        adminEntity = adminDao.findById(id);
        //向界面文本框中注入数据
        adminName.setText(adminEntity.getName());
        adminPassword.setText(adminEntity.getPassword());
//            adminSex.setText(adminEntity.getSex()+"");
        if (adminEntity.getSex() == 1) sexMan.setChecked(true);
        else sexWoman.setChecked(true);
        if (adminEntity.getLevel() == 1) superLevelAdmin.setChecked(true);
        else levelAdmin.setChecked(true);
        adminAddDate.setText(adminEntity.getAddDate());
        adminModifyDate.setText(adminEntity.getModifyDate());
    }
}
